package com.yuzhitong.shapi.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.yuzhitong.shapi.R;
import com.yuzhitong.shapi.util.LoggerUtil;


public abstract class BaseLayoutFragment extends Fragment {
    private LinearLayout basePageBox;
    protected View contentView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.base_fragment_layout, null);
        basePageBox = view.findViewById(R.id.base_fragment_content);
        initView();
        if (contentView != null && contentView.getParent() == null) {
            basePageBox.addView(contentView);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            contentView.setLayoutParams(layoutParams);
        }

        initData();
        return view;
    }

    protected abstract void initView();

    protected abstract void initData();

    public void setContentViewId(@LayoutRes int layoutIds) {
        contentView = View.inflate(getContext(), layoutIds, null);
        if (basePageBox != null) {
            basePageBox.addView(contentView);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            contentView.setLayoutParams(layoutParams);
        }
    }

    public void setContentView(View layout) {
        if (layout == null) {
            LoggerUtil.e("空View");
            return;
        }
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(layoutParams);
        basePageBox.addView(layout);
    }

    protected <T extends View> T findViewById(@IdRes int layoutId) {
        return basePageBox.findViewById(layoutId);
    }
}
